package cn.dankal.basiclib.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.dankal.basiclib.pojo.CategoryListEntity;
import cn.dankal.basiclib.pojo.GroupInvateCountBean;
import cn.jmessage.support.google.gson.Gson;
import cn.jmessage.support.google.gson.reflect.TypeToken;

public class SharePreferenceManager {
    static SharedPreferences sp;

    public static void init(Context context, String name) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    private static final String KEY_CACHED_USERNAME = "jchat_cached_username";

    public static void setCachedUsername(String username) {
        if (null != sp) {
            sp.edit().putString(KEY_CACHED_USERNAME, username).apply();
        }
    }

    public static String getCachedUsername() {
        if (null != sp) {
            return sp.getString(KEY_CACHED_USERNAME, null);
        }
        return null;
    }

    private static final String KEY_CACHED_PSW = "jchat_cached_psw";

    public static void setCachedPsw(String psw) {
        if (null != sp) {
            sp.edit().putString(KEY_CACHED_PSW, psw).apply();
        }
    }

    public static String getCachedPsw() {
        if (null != sp) {
            return sp.getString(KEY_CACHED_PSW, null);
        }
        return null;
    }

    private static final String KEY_REGISTER_USERNAME = "key_register_username";

    public static void setRegisterUsername(String username) {
        if (null != sp) {
            sp.edit().putString(KEY_REGISTER_USERNAME, username).apply();
        }
    }

    public static String getRegistrUsername() {
        if (null != sp) {
            return sp.getString(KEY_REGISTER_USERNAME, null);
        }
        return null;
    }

    private static final String REGISTER_NAME = "register_name";

    public static void setRegisterName(String username) {
        if (null != sp) {
            sp.edit().putString(REGISTER_NAME, username).apply();
        }
    }

    public static String getRegistrName() {
        if (null != sp) {
            return sp.getString(REGISTER_NAME, null);
        }
        return null;
    }

    private static final String REGISTER_PASS = "register_pass";

    public static void setRegistePass(String pass) {
        if (null != sp) {
            sp.edit().putString(REGISTER_PASS, pass).apply();
        }
    }

    public static String getRegistrPass() {
        if (null != sp) {
            return sp.getString(REGISTER_PASS, null);
        }
        return null;
    }

    private static final String ADD_FRIEND_ITEM = "item";

    public static void setItem(Long pass) {
        if (null != sp) {
            sp.edit().putLong(ADD_FRIEND_ITEM, pass).apply();
        }
    }

    public static Long getItem() {
        if (null != sp) {
            return sp.getLong(ADD_FRIEND_ITEM, 0);
        }
        return null;
    }

    private static final String KEY_CACHED_AVATAR_PATH = "jchat_cached_avatar_path";

    public static void setCachedAvatarPath(String path) {
        if (null != sp) {
            sp.edit().putString(KEY_CACHED_AVATAR_PATH, path).apply();
        }
    }

    public static String getCachedAvatarPath() {
        if (null != sp) {
            return sp.getString(KEY_CACHED_AVATAR_PATH, null);
        }
        return null;
    }

    private static final String CONVERSATION_TOP = "conversation_top";

    public static void setTopSize(int num) {
        if (null != sp) {
            sp.edit().putInt(CONVERSATION_TOP, num).apply();
        }
    }

    public static int getTopSize() {
        if (null != sp) {
            return sp.getInt(CONVERSATION_TOP, 0);
        }
        return 0;
    }

    private static final String CONVERSATION_TOP_CANCEL = "conversation_top_cancel";

    public static void setCancelTopSize(int num) {
        if (null != sp) {
            sp.edit().putInt(CONVERSATION_TOP_CANCEL, num).apply();
        }
    }

    public static int getCancelTopSize() {
        if (null != sp) {
            return sp.getInt(CONVERSATION_TOP_CANCEL, 0);
        }
        return 0;
    }


    private static final String KEY_REGISTER_AVATAR_PATH = "jchat_register_avatar_path";

    public static void setRegisterAvatarPath(String path) {
        if (null != sp) {
            sp.edit().putString(KEY_REGISTER_AVATAR_PATH, path).apply();
        }
    }

    public static String getRegisterAvatarPath() {
        if (null != sp) {
            return sp.getString(KEY_REGISTER_AVATAR_PATH, null);
        }
        return null;
    }

    private static final String KEY_CACHED_FIX_PROFILE_FLAG = "fixProfileFlag";

    public static void setCachedFixProfileFlag(boolean value) {
        if (null != sp) {
            sp.edit().putBoolean(KEY_CACHED_FIX_PROFILE_FLAG, value).apply();
        }
    }

    public static boolean getCachedFixProfileFlag() {
        return null != sp && sp.getBoolean(KEY_CACHED_FIX_PROFILE_FLAG, false);
    }

    private static final String NO_DISTURB = "no_disturb";

    public static void setNoDisturb(boolean value) {
        if (null != sp) {
            sp.edit().putBoolean(NO_DISTURB, value).apply();
        }
    }

    public static boolean getNoDisturb() {
        return null != sp && sp.getBoolean(NO_DISTURB, false);
    }

    private static final String IS_SHOWCHECK = "isShowCheck";

    public static void setShowCheck(boolean value) {
        if (null != sp) {
            sp.edit().putBoolean(NO_DISTURB, value).apply();
        }
    }

    public static boolean getShowCheck() {
        return null != sp && sp.getBoolean(NO_DISTURB, false);
    }

    private static final String ISOPEN = "isopen";

    public static void setIsOpen(boolean value) {
        if (null != sp) {
            sp.edit().putBoolean(ISOPEN, value).apply();
        }
    }

    public static boolean getIsOpen() {
        return null != sp && sp.getBoolean(ISOPEN, false);
    }

    private static final String SOFT_KEYBOARD_HEIGHT = "SoftKeyboardHeight";

    public static void setCachedKeyboardHeight(int height) {
        if (null != sp) {
            sp.edit().putInt(SOFT_KEYBOARD_HEIGHT, height).apply();
        }
    }

    public static int getCachedKeyboardHeight() {
        if (null != sp) {
            return sp.getInt(SOFT_KEYBOARD_HEIGHT, 0);
        }
        return 0;
    }

    private static final String WRITABLE_FLAG = "writable";

    public static void setCachedWritableFlag(boolean value) {
        if (null != sp) {
            sp.edit().putBoolean(WRITABLE_FLAG, value).apply();
        }
    }

    public static boolean getCachedWritableFlag() {
        return null == sp || sp.getBoolean(WRITABLE_FLAG, true);
    }

    private static final String CACHED_APP_KEY = "CachedAppKey";

    public static void setCachedAppKey(String appKey) {
        if (null != sp) {
            sp.edit().putString(CACHED_APP_KEY, appKey).apply();
        }
    }

    public static String getCachedAppKey() {
        if (null != sp) {
            return sp.getString(CACHED_APP_KEY, "default");
        }
        return "default";
    }

    private static final String CACHED_NEW_FRIEND = "CachedNewFriend";
    private static final String CACHED_NEW_GROUP = "CachedNewGroup";
    private static final String SHOP_CAR_NUM = "ShopCarNum";
    private static final String CACHED_NEW_GROUP_INVATE = "CachedNewGroupInvate";

    public static void setCachedNewGroupNum(int num) {
        if (null != sp) {
            sp.edit().putInt(CACHED_NEW_GROUP, num).apply();
        }
    }

    public static int getCachedNewGroupNum() {
        if (null != sp) {
            return sp.getInt(CACHED_NEW_GROUP, 0);
        }
        return 0;
    }


    public static ArrayList<GroupInvateCountBean> getCachedNewGroupInvate(){
        if(null != sp){
            String str = sp.getString(CACHED_NEW_GROUP_INVATE,null);
            if(!TextUtils.isEmpty(str)){
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<GroupInvateCountBean>>() {
                }.getType();
                ArrayList<GroupInvateCountBean> list = gson.fromJson(str, type);
                return list;
            }
            return null;
        }
        return null;
    }

    public static int getCachedNewGroupInvateNumByGid(long gid){
        if(null != sp){
            ArrayList<GroupInvateCountBean> list = getCachedNewGroupInvate();
            if(list != null) {
                for (GroupInvateCountBean item : list) {
                    if (item.gid == gid) {
                        return item.count;
                    }
                }
            }else
                return 0;
        }
        return 0;
    }

    public static void removeCachedNewGroupInvateNumByGid(long gid){
        if(null != sp) {
            ArrayList<GroupInvateCountBean> list = getCachedNewGroupInvate();
            if (list != null) {
                GroupInvateCountBean needRemove = null;
                for (GroupInvateCountBean item : list) {
                    if (item.gid == gid) {
                        needRemove = item;
                        break;
                    }
                }
                if(needRemove != null)
                    list.remove(needRemove);
                String json = new Gson().toJson(list);
                sp.edit().putString(CACHED_NEW_GROUP_INVATE,json).apply();
            }
        }
    }

    public static void reduceCachedNewGroupInvateNumByGid(long gid){
        if(null != sp) {
            ArrayList<GroupInvateCountBean> list = getCachedNewGroupInvate();
            if (list != null) {
                GroupInvateCountBean needRemove = null;
                for (GroupInvateCountBean item : list) {
                    if (item.gid == gid) {
                        needRemove = item;
                        break;
                    }
                }
                if(needRemove != null){
                    if(needRemove.count <= 1){
                        list.remove(needRemove);
                    } else
                        needRemove.count -= 1;
                    String json = new Gson().toJson(list);
                    sp.edit().putString(CACHED_NEW_GROUP_INVATE,json).apply();
                }
            }
        }
    }

    public static void addCachedNewGroupInvate(GroupInvateCountBean bean){
        if(null != sp){
            String str = sp.getString(CACHED_NEW_GROUP_INVATE,null);
            if(!TextUtils.isEmpty(str)){
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<GroupInvateCountBean>>() {
                }.getType();
                ArrayList<GroupInvateCountBean> list = gson.fromJson(str, type);
                if(list != null){
                    if(list.size()>0){
                        boolean match = false;
                        for(GroupInvateCountBean item : list){
                            if(item.gid == bean.gid){
                                match = true;
                                item.count ++;
                                break;
                            }
                        }
                        if(!match){
                            list.add(bean);
                        }
                        String json = new Gson().toJson(list);
                        sp.edit().putString(CACHED_NEW_GROUP_INVATE,json).apply();
                    }else{
                        ArrayList<GroupInvateCountBean> list1 = new ArrayList<>();
                        list1.add(bean);
                        String json = new Gson().toJson(list1);
                        sp.edit().putString(CACHED_NEW_GROUP_INVATE,json).apply();
                    }
                }else {
                    ArrayList<GroupInvateCountBean> list1 = new ArrayList<>();
                    list1.add(bean);
                    String json = new Gson().toJson(list1);
                    sp.edit().putString(CACHED_NEW_GROUP_INVATE,json).apply();
                }
            }else{
                ArrayList<GroupInvateCountBean> list = new ArrayList<>();
                list.add(bean);
                String json = new Gson().toJson(list);
                sp.edit().putString(CACHED_NEW_GROUP_INVATE,json).apply();
            }
        }
    }

    public static int getCachedNewGroupInvateNum(){
        if(null != sp){
            String str = sp.getString(CACHED_NEW_GROUP_INVATE,null);
            if(!TextUtils.isEmpty(str)){
                Gson gson = new Gson();
                Type type = new TypeToken<ArrayList<GroupInvateCountBean>>() {
                }.getType();
                ArrayList<GroupInvateCountBean> list = gson.fromJson(str, type);
                if(list != null){
                    int num = 0 ;
                    for(GroupInvateCountBean item : list){
                        num = num + item.count;
                    }
                    return num;
                }else
                    return 0;
            }
            return 0;
        }
        return 0;
    }


    public static void setCachedNewFriendNum(int num) {
        if (null != sp) {
            sp.edit().putInt(CACHED_NEW_FRIEND, num).apply();
        }
    }

    public static int getCachedNewFriendNum() {
        if (null != sp) {
            return sp.getInt(CACHED_NEW_FRIEND, 0);
        }
        return 0;
    }

    public static void setShopCarNum(int num) {
        if (null != sp) {
            sp.edit().putInt(SHOP_CAR_NUM, num).apply();
        }
    }

    public static int getShopCarNum() {
        if (null != sp) {
            return sp.getInt(SHOP_CAR_NUM, 0);
        }
        return 0;
    }

    private static final String CACHED_IS_SIGN_IN = "signIn";

    public static void setCachedIsSign(String name,boolean isSignIn) {
        if (null != sp) {
            sp.edit().putBoolean(name, isSignIn).apply();
        }
    }

    public static boolean getCachedIsSign(String name) {
        if (null != sp) {
            return sp.getBoolean(name, false);
        }
        return false;
    }

    private static final String CACHED_SEARCH_HISTORY = "search_store_history";

    public static void setCachedSearchHistory(Set<String> isSignIn) {
        if (null != sp) {
            sp.edit().putStringSet(CACHED_SEARCH_HISTORY, isSignIn).apply();
        }
    }

    public static Set<String> getCachedSearchHistory() {
        if (null != sp) {
            return sp.getStringSet(CACHED_SEARCH_HISTORY, new HashSet<>());
        }
        return new HashSet<>();
    }

    private static final String CACHED_STORE_CATEGORY = "store_category";

    public static void setCachedStoreCategory(List<CategoryListEntity> categoryListEntities) {
        if (null != sp) {
            sp.edit().putString(CACHED_STORE_CATEGORY, JSON.toJSONString(categoryListEntities)).apply();
        }
    }

    public static List<CategoryListEntity> getCachedStoreCategory() {
        if (null != sp) {
            String json = sp.getString(CACHED_STORE_CATEGORY, "");
            return JSON.parseArray(json, CategoryListEntity.class);
        }
        return new ArrayList<>();
    }

    private static final String CACHED_APP_ID = "CachedAppId";

    public static void setCachedAppId(String appId) {
        if (null != sp) {
            sp.edit().putString(CACHED_APP_ID, appId).apply();
        }
    }

    public static String getCachedAppId() {
        if (null != sp) {
            return sp.getString(CACHED_APP_ID, "default");
        }
        return "default";
    }

    private static final String CURRENT_USER = "currentUser";

    public static void setCurrentUser(int type) {
        if (null != sp) {
            sp.edit().putInt(CURRENT_USER, type).apply();
        }
    }

    public static int getCurrentUser() {
        if (null != sp) {
            return sp.getInt(CURRENT_USER, 1003);
        }
        return 1003;
    }
}
